package com.clothing.customization.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.UpdateTimestamp;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 定制订单实体类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "tb_custom_order")
public class CustomOrder {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 订单编号
     */
    @Column(name = "order_no", nullable = false, length = 50, unique = true)
    private String orderNo;
    
    /**
     * 用户ID
     */
    @Column(name = "user_id", nullable = false)
    private Long userId;
    
    /**
     * 设计师ID
     */
    @Column(name = "designer_id")
    private Long designerId;
    
    /**
     * 服装类型
     */
    @Column(name = "clothing_type", nullable = false, length = 50)
    private String clothingType;
    
    /**
     * 性别
     */
    @Column(nullable = false, length = 20)
    private String gender;
    
    /**
     * 标准尺码
     */
    @Column(name = "size_standard", length = 20)
    private String sizeStandard;
    
    /**
     * 详细尺寸，JSON格式
     */
    @Column(name = "size_detail", columnDefinition = "json")
    private String sizeDetail;
    
    /**
     * 材料ID
     */
    @Column(name = "material_id")
    private Long materialId;
    
    /**
     * 颜色
     */
    @Column(length = 20)
    private String color;
    
    /**
     * 风格ID
     */
    @Column(name = "style_id")
    private Long styleId;
    
    /**
     * 特殊要求
     */
    @Column(name = "special_requirements", columnDefinition = "LONGVARCHAR")
    private String specialRequirements;
    
    /**
     * 预算
     */
    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal budget;
    
    /**
     * 最终价格
     */
    @Column(name = "final_price", precision = 10, scale = 2)
    private BigDecimal finalPrice;
    
    /**
     * 预计交付日期
     */
    @Column(name = "delivery_date")
    private LocalDate deliveryDate;
    
    /**
     * 状态
     */
    @Column(nullable = false, length = 20)
    private String status;
    
    /**
     * 支付状态
     */
    @Column(name = "payment_status", length = 20)
    private String paymentStatus;
    
    /**
     * 支付方式
     */
    @Column(name = "payment_method", length = 20)
    private String paymentMethod;
    
    /**
     * 支付时间
     */
    @Column(name = "payment_time")
    private LocalDateTime paymentTime;
    
    /**
     * 进度(0-100)
     */
    private Integer progress;
    
    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "create_time", updatable = false)
    private LocalDateTime createTime;
    
    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "update_time")
    private LocalDateTime updateTime;
    
    /**
     * 新建订单前的操作
     */
    @PrePersist
    public void prePersist() {
        if (orderNo == null) {
            orderNo = generateOrderNo();
        }
        if (status == null) {
            status = "pending"; // 默认待接受状态
        }
        if (paymentStatus == null) {
            paymentStatus = "unpaid"; // 默认未支付
        }
        if (progress == null) {
            progress = 10; // 默认进度10%
        }
    }
    
    /**
     * 生成订单编号
     * 格式：C-yyyyMMdd-xxxx (xxxx为随机字符串)
     */
    private String generateOrderNo() {
        LocalDateTime now = LocalDateTime.now();
        String dateStr = String.format("%tY%<tm%<td", now);
        String randomStr = UUID.randomUUID().toString().substring(0, 4);
        return "C-" + dateStr + "-" + randomStr;
    }
} 